import CrudService from '../global/CrudService'

let instance = null

// 后端路径
const prefix = '/rptTemplate'

class Service extends CrudService {
  constructor() {
    super(prefix)
    this.getListPath = `${prefix}/queryTemplateList`
    this.addPath = `${prefix}/doTemplateInsert`
    this.deletePath = `${prefix}/doTemplateDelete`
    this.updatePath = `${prefix}/doTemplateUpdate`
    return instance || this
  }

  static getInstance() {
    if (!instance) {
      instance = new Service()
    }
    return instance
  }

  async copy(id, template_name) {
    return this.post(`${prefix}/doTemplateCopy`, { id, template_name })
  }

  // // @Override
  // getList = async (model = {}) => {
  //   return this.get(`${prefix}/queryTemplateList`, model)
  // }
  async setStart(id, is_start) {
    const query = this.qs({ id, is_start })
    return this.post(`${prefix}/doTemplateSetStart`, { id, is_start })
  }

  getList = async (model) => {
    const query = {...model, template_name: model.templateName }
    const res = await this.get(this.getListPath, query)
    return { ...res, list: (res.list || []).map(e => ({ ...e, templateName: e.template_name })) }
  }

  /**
   * 新增
   * @param model
   * @returns {Promise<*>}
   */
  addItem = async (model) => {
    return this.post(this.addPath, { ...model, template_name: model.templateName })
  }

  /**
   * 编辑
   * @param model object
   * @returns {Promise<*>}
   */
  editItem = async (model) => {
    return this.post(this.updatePath, { ...model, template_name: model.templateName })
  }

}

export default Service.getInstance()
